package chuangyi.com.org.DOMIHome;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by zxh on 16/12/23.
 */


public class AlxTextView extends TextView {
    private int lastHeight, lastWidth;
    private Context context;

    public AlxTextView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        this.context = context;
        setSingleLine(false);
    }

    public AlxTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.context = context;
        setSingleLine(false);
    }

    public AlxTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        this.context = context;
        setSingleLine(false);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (super.getMeasuredHeight() != lastHeight || super.getMeasuredWidth() != lastWidth) {
            super.setMinHeight(super.getMeasuredHeight());
            super.setMaxHeight(Integer.MAX_VALUE);
        }
        lastHeight = super.getMeasuredHeight();
        lastWidth = super.getMeasuredWidth();
//        super.setMaxLines();
//        getParent().requestLayout();
//
//        if (layout != null) {
//            int height = (int) Math.ceil(getMaxLineHeight(layout.getLineCount()))
//                    + getCompoundPaddingTop() + getCompoundPaddingBottom();
//            int width = getMeasuredWidth();
//            setMeasuredDimension(width, height);
//        }
    }

    private float getMaxLineHeight(int lineCount) {
        float height = 0.0f;
//        float screenW = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth();
//        float paddingLeft = ((LinearLayout) this.getParent()).getPaddingLeft();
//        float paddingReft = ((LinearLayout) this.getParent()).getPaddingRight();
////这里具体this.getPaint()要注意使用，要看你的TextView在什么位置，这个是拿TextView父控件的Padding的，为了更准确的算出换行
//        int line = (int) Math.ceil((this.getPaint().measureText(str) / (screenW - paddingLeft - paddingReft)));
        height = (this.getPaint().getFontMetrics().descent - this.getPaint().getFontMetrics().ascent) * lineCount;
        return height;
    }
}
